package com.javaee.controller.admin;


import com.javaee.BeforeBaseController;
import com.javaee.entity.Record;

import com.javaee.service.admin.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mo
 * @since 2024-11-27
 */
@Controller
@RequestMapping("/admin")
public class RecordController extends BeforeBaseController {
@Autowired
 private RecordService recordService;
    @RequestMapping("/recordList")
    public String record(String keyword,Model model, Integer currentPage){
        if(keyword == null){
            keyword = "";
        }
        if (currentPage == null) {
            currentPage = 1;
        }
        int totalCount = recordService.selectCount();
        System.out.println("totalCount = " + totalCount);
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
       List<Record> records = recordService.searchRecord( keyword,(currentPage-1)*pageSize,pageSize);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("records",records);

return "/admin/recordList";
    }
    //添加就诊记录
    @RequestMapping("/toAddRecord")
    public String toAddRecord(){

        return "/admin/addRecord";
    }
    @PostMapping("/addRecord")
    public String addRecord(Record record){
        recordService.addRecord(record);
return "redirect:/admin/recordList";
    }
    //删除就诊记录
    @RequestMapping("/deleteRecord")
    @ResponseBody
    public String deleteBoard(Integer id, Model model) {
            return recordService.deleteRecord(id) ? "成功":"失败";

    }
    //修改就诊记录
    @GetMapping("/toUpdateRecord/{id}")
    public String toUpdateRecord(@PathVariable("id") Integer id,Model model){
        Record record = recordService.getRecordById(id);
        model.addAttribute("record",record);
        return "/admin/updateRecord";
    }

    @PostMapping("/updateRecord")
    public String updateRecord(Record record){
        if (recordService.updateRecord(record)){
            return "redirect:/admin/recordList";
        }
        else {
            return "redirect:/admin/toUpdateBoard/"+record.getId();
        }
    }
    //模糊搜索
//    @PostMapping("/searchRecord")
//    public String searchRecord(@RequestParam String keyword,Model model){
//        List<Record> records = recordService.searchRecord(keyword);
//        model.addAttribute("records",records);
//        return "/admin/recordList";
//    }
}
